package com.shawn.admin.listener;

import com.alibaba.fastjson.JSON;
import com.shawn.admin.mapper.LogMapper;
import com.shawn.autoconfig.template.GreenTemplate;
import com.shawn.dubbo.api.MovementApi;
import com.shawn.model.entity.Log;
import com.shawn.model.mongo.Movement;
import io.jsonwebtoken.lang.Strings;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Component
public class SpringListener {

    @Autowired
    private LogMapper logMapper;

    @DubboReference
    private MovementApi movementApi;

    @Autowired
    private GreenTemplate greenTemplate;

    /**
     * 监听所有的日志记录
     */
    @RabbitListener(bindings = @QueueBinding(
        value =@Queue(name = "touch.log.queue"),//队列名
            exchange = @Exchange(name = "touch.log.exchange",type = ExchangeTypes.TOPIC),//交换机名,交换机类型
            key ={"log.*"}//通讯规则
    ))
    public void LogListener(String message){
        log.info("接收到消息:{}",message);
        //解析内容
        try {
            Map map = JSON.parseObject(message, Map.class);
            //1、获取数据
            Long userId = Long.parseLong((String) map.get("userId"));
            String logTime = (String) map.get("logTime");
            String busId = (String) map.get("busId");
            String type = (String) map.get("type");
            Log log = new Log(userId, logTime, type);
            logMapper.insert(log);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 动态审核消息
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "touch.audit.queue"),
            exchange = @Exchange(name = "touch.audit.exchange",type = ExchangeTypes.TOPIC),
            key = {"audit.movement"}
    ))
    public void AuditListener(String movementId){
        log.info("接收到消息:{}",movementId);
        //根据id查询动态
        Movement movement = movementApi.getById(movementId);
        try {
            if (movement!=null&&movement.getState()==0) {
                Map<String, String> txtMap = greenTemplate.greenTxtScan(movement.getTextContent());
                Map imgMap = greenTemplate.greenImageScan(movement.getMedias());
                if (!Objects.isNull(txtMap)&&!Objects.isNull(imgMap)) {
                    //审核结果均不为空
                    int state = 0;
                    String txtSuggestion = txtMap.get("suggestion");
                    String imgSuggestion = (String) imgMap.get("suggestion");
                    if ("block".equals(txtSuggestion)||"block".equals(imgSuggestion)) {
                        //有一项未通过 驳回
                        state=2;
                    }else if ("pass".equals(txtSuggestion)&&"pass".equals(imgSuggestion)){
                        //通过
                        state=1;
                    }
                    movement.setState(state);
                    movementApi.update(movement);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
